package com.delio.client.produccion.articulos;

import java.util.ArrayList;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.produccion.tipos.Tipo;
import com.delio.client.produccion.tipos.Tipos;
import com.delio.client.produccion.unidades.Unidad;
import com.delio.client.produccion.unidades.Unidades;
import com.delio.client.util.ComboboxFieldBinding;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.custom.Portal;
import com.extjs.gxt.ui.client.widget.custom.Portlet;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;

public class ArticuloForm extends LayoutContainer{
	private DelioConstants x = GWT.create(DelioConstants.class);
	EditorGrid<ModelData> gtipart,gund;
	Grid<ModelData> gart;
	Button btnSaveTipArt,btnNewTipArt,btnDeleteTipArt,btnSaveUND,btnNewUND,btnDeleteUND,btnSaveART,btnNewART,btnDeleteART,btnUpdateART,guardarART,salirART;
	private BeanModel selectedModelUND,selectedModelTipArt,selectedModelART;
	TextField<String>  txtDescripcionTipArt,txtDescripcionUND,codArt;
	TextArea taDescripcionArt;
	FormBinding fbart;
	Dialog dl;
	FormPanel fp;
	HiddenField<String> id;
	ComboBox<ModelData> tart,und;
	Integer idtart,idund;
    PagingToolBar toolBarPagART;
	
	
	public ArticuloForm(){
		
	    setLayout(new BorderLayout());  
	    LayoutContainer north = new LayoutContainer();  
	    BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 30);  
	  
	   /* ContentPanel west = new ContentPanel();
	   
	    west.setBodyBorder(false);  
	    west.setHeading("West");  
	    west.setLayout(new AccordionLayout());  
	  
	    ContentPanel nav = new ContentPanel();  
	    nav.setHeading("Navigation");  
	    nav.setBorders(false);  
	    nav.setBodyStyle("fontSize: 12px; padding: 6px"); 
	    nav.add(new Button("Nuevo"));
	    nav.addText("HOllllllllla");  
	    west.add(nav);  
	  
	    ContentPanel settings = new ContentPanel();  
	    settings.setHeading("Settings");  
	    settings.setBorders(false);  
	    west.add(settings); 
	  
	    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200, 100, 300);  
	    westData.setMargins(new Margins(5, 0, 5, 5));  
	    westData.setCollapsible(true);*/   
	  
	    Portal portal = new Portal(3);  
	    portal.setBorders(true);  
	    portal.setStyleAttribute("backgroundColor", "SkyBlue");  
	    portal.setColumnWidth(0, .50);  
	    portal.setColumnWidth(1, .50);  
	    portal.setColumnWidth(2, .50);  
				      
	   	Portlet portlet = new Portlet();  
		Tipos.listartipos();
	    portlet.setHeading("Tipo Articulos"); 
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridTipArt());
	    configPanelTipArt(portlet);  
	    portlet.setHeight(200);
	    portal.add(portlet, 0);  
	  
	    portlet = new Portlet();  
		Unidades.listarestadoUnidades();
	    portlet.setHeading("Unidades");  
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridUND());
	    configPanelUND(portlet);  
	    portlet.setHeight(200);
	    portal.add(portlet, 1);  
	    
	    portlet = new Portlet();  

		Articulos.getPaginate();
	    Tipos.listartiposCombo();
	    Unidades.listarestadoUnidadesCombo();  
	    FormART();
	    portlet.setHeading("Articulos");  
	    portlet.setLayout(new FitLayout());
	    portlet.add(createGridART());
	    configPanelART(portlet);  
	    portlet.setHeight(300);
	    portal.add(portlet, 2);
	   
		  
	  
	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5));  
	  
	    add(north, northData);  
	    //add(west, westData);  
	    add(portal, centerData);  
	  }  
	   	
	  public Grid<ModelData> createGridTipArt() {  

		  	List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
			
			CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
		    conf.add(sm.getColumn());
		    
		    ColumnConfig col = new ColumnConfig("descripcion","Descripcion",200);
		    
		    txtDescripcionTipArt = new TextField<String>();  
		    txtDescripcionTipArt.setAllowBlank(false);  
		    col.setEditor(new CellEditor(txtDescripcionTipArt));  
		    conf.add(col);  
		  
		    ColumnModel cm = new ColumnModel(conf); 
		    
		    gtipart = new EditorGrid<ModelData>(Tipos.store, cm); 
		    gtipart.setLoadMask(true); 
		    gtipart.setBorders(true); 
		    gtipart.addPlugin(sm);

		    gtipart.setSelectionModel(sm);
		    gtipart.getSelectionModel().addListener(Events.SelectionChange,
		       new Listener<SelectionChangedEvent<ModelData>>() {
				@Override
				public void handleEvent(SelectionChangedEvent<ModelData> be) {
				  if(be.getSelection().size()==0){
					btnDeleteTipArt.setEnabled(false);
					btnSaveTipArt.setEnabled(false);
					btnNewTipArt.setEnabled(true);
			       }
				  if(be.getSelection().size()==1){
				 // be.getSelection().get(0);
				    selectedModelTipArt = (BeanModel) be.getSelectedItem();
				    Tipos.tipo_id = selectedModelTipArt.get("id");
				    System.out.println("el id del tipo "+Tipos.tipo_id);
			        btnDeleteTipArt.setEnabled(true);
				    btnSaveTipArt.setEnabled(true);
					btnNewTipArt.setEnabled(false);
				  }
		 		}
		    });	
		    gtipart.setBorders(true);  
		    return gtipart;  
		  }    	
			
		private void configPanelTipArt(final ContentPanel panel) {  
			  panel.setCollapsible(true);  
			  panel.setAnimCollapse(false);  
			  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
			  panel.getHeader().addTool(  
			      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
			        @Override  
			         public void componentSelected(IconButtonEvent ce) {  
			           panel.removeFromParent();  
			         }  
			     }));  
			   panel.setTopComponent(toolbarTipArt());
		    }
			
			public ToolBar toolbarTipArt(){
			  ToolBar toolBar = new ToolBar(); 
			  btnNewTipArt = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
			  btnSaveTipArt = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
			  btnDeleteTipArt = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
			  btnSaveTipArt.setEnabled(false);
			  btnDeleteTipArt.setEnabled(false);
			  
			  toolBar.add(btnNewTipArt);
			  toolBar.add(btnSaveTipArt);
			  toolBar.add(btnDeleteTipArt);
		      
		      btnDeleteTipArt.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
	       		if(Tipos.tipo_id != null){
		       		System.out.println("tttttttttooooooooooodoooooooo");
					Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
					  public void handleEvent(MessageBoxEvent be) {
				        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
					       Tipos.eliminar(Tipos.tipo_id);
					       Tipos.loader.load();
					      }
				        }
					  };
					    String msg = "Esta seguro que desea eliminar este Articulos?";
					    MessageBox.confirm(x.eliminarCliente(), msg, callback);
					  }else{
					    Tipos.store.remove(0);
					}
		       	   }
		    	});
		      btnNewTipArt.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    	    Tipo t = new Tipo();
					t.setDescripcion("");
					BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(t.getClass());
					gtipart.stopEditing();
				
					Tipos.store.insert(beanModelFactory.createModel(t), 0);
					gtipart.startEditing(Tipos.store.indexOf(beanModelFactory.createModel(t)), 0);
		    	}
		    	});
		        btnSaveTipArt.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    		Tipo  t = new Tipo();
			 		t.setDescripcion(txtDescripcionTipArt.getValue()); 
			 		btnSaveTipArt.setEnabled(false);	  
			 		if(Tipos.tipo_id==null){
			 		  Tipos.guardar(t);
			 		}
			 		if(Tipos.tipo_id!=null){
			 		  t.setId(Tipos.tipo_id);
				 	  Tipos.actualizar(t);
			 		}
			 		 Tipos.store.commitChanges();
			 		 Tipos.loader.load();
			 		 Tipos.loaderCombo.load();
		    	}
		       });
		     return toolBar;
		    }	

			
	  public Grid<ModelData> createGridUND() {  
		
		  
			List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
			
			CheckBoxSelectionModel<ModelData> sm = new CheckBoxSelectionModel<ModelData>();
		    conf.add(sm.getColumn());
		  
		    ColumnConfig col = new ColumnConfig("descripcion","Descripcion",200);
			    
		    txtDescripcionUND = new TextField<String>();  
		    txtDescripcionUND.setAllowBlank(false);  
		    col.setEditor(new CellEditor(txtDescripcionUND));  
		    conf.add(col);  
		  
		    ColumnModel cm = new ColumnModel(conf); 
		    
		    gund = new EditorGrid<ModelData>(Unidades.store, cm); 
		    gund.setLoadMask(true); 
		    gund.setBorders(true); 
		    gund.addPlugin(sm);

		    gund.setSelectionModel(sm);
		    gund.getSelectionModel().addListener(Events.SelectionChange,
		       new Listener<SelectionChangedEvent<ModelData>>() {
				@Override
				public void handleEvent(SelectionChangedEvent<ModelData> be) {
			      if(be.getSelection().size()==0){
				      btnDeleteUND.setEnabled(false);
					  btnSaveUND.setEnabled(false);
					  btnNewUND.setEnabled(true);
		           }
				  if(be.getSelection().size()==1){
					 // be.getSelection().get(0);
				      selectedModelUND = (BeanModel) be.getSelectedItem();
				      Unidades.unidad_id = selectedModelUND.get("id");
				      System.out.println("el id de la unidad "+Unidades.unidad_id);
		              btnDeleteUND.setEnabled(true);
					  btnSaveUND.setEnabled(true);
					  btnNewUND.setEnabled(false);
			       }
		 		}
		    });	
		    gund.setBorders(true);  
		    return gund;  
		  }    	
			
		private void configPanelUND(final ContentPanel panel) {  
			  panel.setCollapsible(true);  
			  panel.setAnimCollapse(false);  
			  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
			  panel.getHeader().addTool(  
			      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
			        @Override  
			         public void componentSelected(IconButtonEvent ce) {  
			           panel.removeFromParent();  
			         }  
			     }));  
			   panel.setTopComponent(toolbarUND());
		    }
			
		public ToolBar toolbarUND(){
			  ToolBar toolBar = new ToolBar(); 
			  btnNewUND = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
			  btnSaveUND = new Button("Guardar", IconHelper.createPath("images/icons/save_all.png"));
			  btnDeleteUND = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
			  btnSaveUND.setEnabled(false);
			  btnDeleteUND.setEnabled(false);
			  
			  
			  toolBar.add(btnNewUND);
			  toolBar.add(btnSaveUND);
			  toolBar.add(btnDeleteUND);
		      
		      btnDeleteUND.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
	       		 if(Unidades.unidad_id != null){
		       		 System.out.println("tttttttttooooooooooodoooooooo");
					 Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
					  public void handleEvent(MessageBoxEvent be) {
				        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
					       Unidades.eliminar(Unidades.unidad_id);
					       Unidades.loader.load();
					      }
				        }
					  };
					    String msg = "Esta seguro que desea eliminar el Tipo Documentos?";
					    MessageBox.confirm(x.eliminarCliente(), msg, callback);
					  }else{
					  //TipoDocumentos.storeCombo.rejectChanges();//elimina los datos almacenados en memoria
					   Unidades.store.remove(0);
					}
	        	}
		    	});
		      btnNewUND.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    		Unidad und = new Unidad();
					und.setDescripcion("");
					BeanModelFactory beanModelFactory =  BeanModelLookup.get().getFactory(und.getClass());
					gund.stopEditing();
				
					Unidades.store.insert(beanModelFactory.createModel(und), 0);
					gund.startEditing(Unidades.store.indexOf(beanModelFactory.createModel(und)), 0);
			  	 }
		    	});
		        btnSaveUND.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
	    		   Unidad und = new Unidad();
	    		   und.setDescripcion(txtDescripcionUND.getValue()); 
		 		   btnSaveUND.setEnabled(false);	  
		 		  if(Unidades.unidad_id==null){
		 		   Unidades.guardar(und);
		 		   }
		 		  if(Unidades.unidad_id!=null){
		 		   und.setId(Unidades.unidad_id);
			 	   Unidades.actualizar(und);
		 		   }
		 		   Unidades.store.commitChanges();
		 		   Unidades.loader.load();	
		 		   Unidades.loaderCombo.load();
	    	     }
		       });
		     return toolBar;
		    }

	 public Grid<ModelData> createGridART() {  
			
		    toolBarPagART = new PagingToolBar(10);
	        toolBarPagART.bind(Articulos.loader);
	        
			List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
			
			final CheckBoxSelectionModel<ModelData> smart = new CheckBoxSelectionModel<ModelData>();
		    conf.add(smart.getColumn());
		                      
		    ColumnConfig col = new ColumnConfig("codarticulo","Codigo",100);
		    conf.add(col);  
		    col = new ColumnConfig("tartdescripcion","Tipo Articulo",100);
			conf.add(col);  
		    col = new ColumnConfig("descripcion","Descripcion",100);
			conf.add(col);  
			col = new ColumnConfig("unddescripcion","Unidad",100);
		    conf.add(col);  
		 
		    ColumnModel cm = new ColumnModel(conf); 
		    
		    gart = new Grid<ModelData>(Articulos.store, cm);
		    gart.addListener(Events.Attach, new Listener<GridEvent<ModelData>>() {  
		          public void handleEvent(GridEvent<ModelData> be) {  
		        	  Articulos.loader.load(0, 10);  
		          }  
		        }); 


		    smart.setSelectionMode(SelectionMode.SINGLE);
		    gart.setSelectionModel(smart);
		    gart.getSelectionModel().addListener(Events.SelectionChange,
		       new Listener<SelectionChangedEvent<ModelData>>() {
				@Override
				public void handleEvent(SelectionChangedEvent<ModelData> be) {
			      if(be.getSelection().size()==0){
				      btnDeleteART.setEnabled(false);
					  btnUpdateART.setEnabled(false);
					  btnNewART.setEnabled(true);
		           }
				  if(be.getSelection().size()==1){
					 // be.getSelection().get(0);
					  //fbdoc.bind((BeanModel) be.getSelection().get(0));
				      selectedModelART = (BeanModel) be.getSelectedItem();
				      Articulos.articulo_id = selectedModelART.get("id");
				      System.out.println("el id del articulo  "+Articulos.articulo_id);
		              btnDeleteART.setEnabled(true);
					  btnUpdateART.setEnabled(true);
					  btnNewART.setEnabled(false);
			       }
					if(be.getSelection().size()>1)
		    		  fbart.unbind();
		 		}
		    });
		    
		    gart.addPlugin(smart);
		    gart.setBorders(true);  
		    gart.setLoadMask(true);
		
		    GridFilters filters = new GridFilters();
	        StringFilter codarticuloFilter = new StringFilter("codarticulo");        
	        StringFilter tartdescripcionFilter = new StringFilter("tartdescripcion");
	        StringFilter descripcionFilter = new StringFilter("descripcion");
	        StringFilter unddescripcionFilter = new StringFilter("unddescripcion");
	          
	        filters.addFilter(codarticuloFilter);        
	        filters.addFilter(tartdescripcionFilter);        
	        filters.addFilter(descripcionFilter);        
	        filters.addFilter(unddescripcionFilter);
	         
	        gart.addPlugin(filters);                
	        
           
	        fbart = new FormBinding(fp);
	        fbart.setStore(Articulos.store);
	        fbart.addFieldBinding(new FieldBinding(id,"id"));
	        fbart.addFieldBinding(new FieldBinding(codArt,"codarticulo"));
	        fbart.addFieldBinding(new ComboboxFieldBinding(tart,"tartdescripcion"));
		    fbart.addFieldBinding(new ComboboxFieldBinding(und,"unddescripcion"));
		    fbart.addFieldBinding(new FieldBinding(taDescripcionArt,"descripcion"));
		   
		    return gart;  
		  }    	
			
		private void configPanelART(final ContentPanel panel) {  
			  panel.setCollapsible(true);  
			  panel.setAnimCollapse(false);  
			  panel.getHeader().addTool(new ToolButton("x-tool-gear"));  
			  panel.getHeader().addTool(  
			      new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  
			        @Override  
			         public void componentSelected(IconButtonEvent ce) {  
			           panel.removeFromParent();  
			         }  
			     }));  
			   panel.setTopComponent(toolbarART());
			   panel.setBottomComponent(toolBarPagART);
		    }
			
		public ToolBar toolbarART(){
			  ToolBar toolBar = new ToolBar(); 
			  btnNewART = new Button("Nuevo", IconHelper.createPath("images/icons/new.gif"));
			  btnUpdateART = new Button("Actualizar", IconHelper.createPath("images/icons/update.png"));
			  btnDeleteART = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
			  btnUpdateART.setEnabled(false);
			  btnDeleteART.setEnabled(false);
			  
			  
			  toolBar.add(btnNewART);
			  toolBar.add(btnUpdateART);
			  toolBar.add(btnDeleteART);
 
			/*  dl = new Dialog();  
			  dl.setBodyBorder(false);  
			  dl.setHeading("Nuevo Articulo");  
			  dl.setWidth(350);  
			  dl.setHeight(240);
			  dl.setIconStyle("images/icons/new.gif");
			  dl.setModal(true);
				   
			  Button salirART   = new Button("Salir",IconHelper.create("images/icons/exit.png"));
			  Button guardarART = new Button("Guardar",IconHelper.create("images/icons/save_all.png"));
				  
			  dl.setButtons("");
			  dl.addButton(salirART);
			  dl.addButton(guardarART);
		      dl.setFrame(true);
		      
		      guardarART.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
		       	 if(validForm()){
		       	   Articulo art = new Articulo();
		       	    art.setCodarticulo(codArt.getValue());
		       	    art.setTipo_id(idtart);
		       	    art.setUnidad_id(idund);
		       	    art.setDescripcion(taDescripcionArt.getValue());
		       	    art.setStockfisico(0.0);
		       	   
	       		    btnUpdateART.setEnabled(false);	  
		            if(id.getValue()==null){
		              Articulos.guardar(art);
		            }
		            if(id.getValue()!=null){
		              art.setId(Integer.parseInt(id.getValue().toString()));
				      Articulos.actualizar(art);
				    }
		           dl.hide();
		           Articulos.loader.load();
		       	  }
	 		   	}
		      });
		      
		      salirART.addSelectionListener(new SelectionListener<ButtonEvent>(){
			     public void componentSelected(ButtonEvent ce){
			     	dl.hide();
		       	 }
			   });*/

		      btnDeleteART.addSelectionListener(new SelectionListener<ButtonEvent>(){
		       	public void componentSelected(ButtonEvent ce){
	       		 if(Articulos.articulo_id != null){
		         	Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
					  public void handleEvent(MessageBoxEvent be) {
				        if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
					       Articulos.eliminar(Articulos.articulo_id);
					       Articulos.loader.load();
					      }
				        }
					  };
					    String msg = "Esta seguro que desea eliminar el Articulo?";
					    MessageBox.confirm(x.eliminarCliente(), msg, callback);
					  }else{
					  //TipoDocumentos.storeCombo.rejectChanges();//elimina los datos almacenados en memoria
					   Articulos.store.remove(0);
					}
	        	}
		    	});
		      btnNewART.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    	   clearForm(); 
		    	   //dl.add(fp);
		    	   //dl.show();
		    	   NewArticuloForm naf = new NewArticuloForm();
		    	   naf.show();
		    	   fbart.unbind();
			  	  }
		    	});
		      
		      btnUpdateART.addSelectionListener(new SelectionListener<ButtonEvent>(){
		    	public void componentSelected(ButtonEvent ce){
		    	   fbart.bind(selectedModelART);
		    	   dl.add(fp);
     	    	   dl.show();	
		       	   Articulo art = new Articulo();
	    		   btnUpdateART.setEnabled(false);	  
		 		  if(Articulos.articulo_id==null){
		 		   Articulos.guardar(art);
		 		   }
		 		  if(Articulos.articulo_id!=null){
		 		   art.setId(Articulos.articulo_id);
			 	   Articulos.actualizar(art);
		 		   }
		 		   Articulos.store.commitChanges();
		 		   Articulos.loader.load();
		 		   
	    	     }
		       });
		     return toolBar;
		}
		
		public void FormART(){
			fp = new FormPanel();
			fp.setHeaderVisible(false);
			fp.setFrame(true);
			id = new HiddenField<String>();
			id.setVisible(false);
			codArt = new TextField<String>();
			codArt.setFieldLabel("Nro Articulo");
			codArt.setEnabled(false);
			codArt.setAllowBlank(true);
			tart = new ComboBox<ModelData>();  
			tart.setEmptyText("Select a state...");  
			tart.setAllowBlank(false);
			tart.setDisplayField("descripcion");  
			tart.setFieldLabel("Tipo Articulo");
			tart.setWidth(150);  
			tart.setStore(Tipos.storeCombo);
			tart.setValueField("descripcion");
			tart.setTypeAhead(true);  
			tart.setTriggerAction(TriggerAction.ALL); 
			tart.addSelectionChangedListener(new SelectionChangedListener<ModelData>(){
			  public void selectionChanged(SelectionChangedEvent<ModelData> se) {
				 if(tart.getValue()!=null){   
					   idtart = Integer.parseInt(tart.getValue().get("id").toString());
				 }
			   }
			});
			taDescripcionArt = new TextArea();
			taDescripcionArt.setFieldLabel("Descripcion");
			taDescripcionArt.setAllowBlank(true);
			und = new ComboBox<ModelData>();  
			und.setEmptyText("Select a state...");  
			und.setAllowBlank(false);
			und.setDisplayField("descripcion");  
			und.setFieldLabel("Unidad");
			und.setWidth(150);  
		    und.setStore(Unidades.storeCombo);
			und.setValueField("descripcion");
			und.setTypeAhead(true);  
			und.setTriggerAction(TriggerAction.ALL); 
			und.addSelectionChangedListener(new SelectionChangedListener<ModelData>(){
			  public void selectionChanged(SelectionChangedEvent<ModelData> se) {
				if(und.getValue()!=null){   
				   idund = Integer.parseInt(und.getValue().get("id").toString());
				 }
			   }
			});	
			fp.add(id);	
			fp.add(codArt);
			fp.add(tart);
			fp.add(und);
			fp.add(taDescripcionArt);
    	}
		
		public boolean validForm(){
		  return fp.isValid();
		}

	   public void clearForm(){
    	 fp.clear();
       }
}